package com.hacker.concurrent.com.hacker.thread.base;

/**
 * @Author:这个程序员有纹身
 * @Date: Created in 20:57 2017\12\19 0019
 * 军队线程
 * 模拟作战双方的行为
 */
public class ArmyRunnable implements Runnable{

    //volatile 保证了线程可以正确的读取其他线程写入的值（可见性JMM , happens-before原则）
    volatile boolean keepRunning = true;

    @Override
    public void run(){
        while(keepRunning){
            //发动5连击
            for (int i = 0; i < 5; i++){
                System.out.println(Thread.currentThread().getName() + "进攻对方【" + i + "】");

                //让出处理器时间，下次进攻双方未知
                Thread.yield();

            }
        }
        System.out.println(Thread.currentThread().getName() + "结束了战斗！");
    }
}
